package com.msun.monitor.entity;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;

@Data
@TableName("alert")
public class Alert implements IParse<Alert> {

    /**
     * 告警ID，唯一ID
     */
    private Integer alertId;

    /**
     * 状态，firing，resaver
     */
    private String status;

    /**
     * 告警名称
     */
    private String alertName;

    /**
     * 告警描述
     */
    private String alertDesc;

    /**
     * 告警分组
     */
    private String alertGroup;

    /**
     * 告警开始时间
     */
    private Date startAt;

    /**
     * 告警结束时间
     */
    private Date endAt;

    /**
     * 告警环境
     */
    private String environment;

    /**
     * 告警原始内容
     */
    private String fullText;

    /**
     * 告警指纹
     */
    private String fingerPrint;

    @Override
    public Alert parse(JSONObject object) {
        JSONObject labels = object.getJSONObject("labels");
        JSONObject annotations = object.getJSONObject("annotations");
        this.setAlertName(labels.getString("alertname"));
        this.setAlertDesc(annotations.getString("description"));
        this.setStatus(object.getString("status"));
        this.setEnvironment(labels.getString("hospital"));
        this.setAlertGroup(labels.getString("alertgroup"));
        this.setStartAt(DateUtils.parseDate(object.getString("startsAt")));
        this.setEndAt(DateUtils.parseDate(object.getString("endsAt")));
        this.setFullText(object.toJSONString());
        this.setFingerPrint(object.getString("fingerprint"));
        return this;
    }
}
